<?php

declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\controller\system;


use app\AdminController;
use app\common\library\Ip2Region;
use app\common\model\system\PointProduct as PointProducts;
use support\Response;
use system\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;

/**
 * 用户管理
 * Class User
 * @package app\admin\controller\system
 */
class Pointproduct extends AdminController
{
    // 初始化函数
    public function __construct()
    {
        parent::__construct();
        $this->model = new PointProducts();
    }

    /**
     * 获取资源
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): \support\Response
    {

        if (request()->isAjax()) {
            // 获取数据
            $post = \request()->all();
            $page = (int)input('page') ?? 1;
            $limit = (int)input('limit') ?? 10;
            $status = !empty($post['status']) ? (int)$post['status'] : [1, 2];
            // 生成查询条件
            $where = array();
            if (!empty($post['title'])) {
                $where[] = ['title', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['type'])) {
                $where[] = ['type', 'in', $post['type']];
            }
            if (!empty($post['ishot'])) {
                $where[] = ['ishot', 'in', $post['ishot']];
            }
            // 生成查询数据
            $where[] = ['status', 'in', $status];
            $count = $this->model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $this->model->where($where)->order("id desc")->limit((int)$limit)->page((int)$page)->select();
            foreach ($list as $lists) {
                $lists['images'] = json_decode($lists['images'], true);
            }
            // TODO..
            return $this->success('查询成功', "", $list, $count);
        }
        return view('/system/admin/Pointproduct/index', []);
    }

    /**
     * 添加会员
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = request()->post();
            $post = request_validate_rules($post, get_class($this->model));
            if (empty($post) || !is_array($post)) {
                return $this->error($post);
            }
            $post['images'] = [];
            $post['album'] = $post['album'] ?? [];
            if (empty($post['album'])) {
                return $this->error('产品图片不能为空');
            }
            foreach ($post['album'] as $value) {
                $post['images'][] = $value['src'];
            }
            $post['images'] = json_encode($post['images']);
            if ($this->model->create($post)) {
                return $this->success('添加成功！');
            }
            return $this->error('添加失败！');
        }
        return view('/system/admin/Pointproduct/add', [
            'data' => $this->getTableFields()
        ]);
    }

    /**
     * 编辑会员
     */
    public function edit()
    {
        $id = input('id');
        if (request()->isPost()) {
            $post = \request()->post();
            // 查询数据
            $data = $this->model->find($post['id']);
            $post['images'] = [];
            $post['album'] = $post['album'] ?? [];
            if (empty($post['album'])) {
                return $this->error('产品图片不能为空');
            }
            foreach ($post['album'] as $value) {
                $post['images'][] = $value['src'];
            }
            $post['images'] = json_encode($post['images']);
            if ($this->model->update($post)) {
                return $this->success('更新成功！');
            }
            return $this->error('更新失败！');
        }
        $data = $this->model->find($id);
        if ($data) {
            $data['images'] = json_decode($data['images'], true);
            $albums = [];
            foreach ($data['images'] as $key => $value) {
                $albums[$key] = [
                    'title' => '图片',
                    'src' => $value
                ];
            }
            $data['album'] = $albums;
        }
        return view('/system/admin/Pointproduct/add', ['data' => $data]);
    }

    /**
     * 删除轮播图
     */
    public function del()
    {
        $id = input('id');
        if (!empty($id) && is_numeric($id)) {
            if ($this->model::destroy($id)) {
                return $this->success('删除成功！');
            }
        }
        return $this->error('删除失败，请检查您的参数！');
    }


    /**
     * 修改显示状态
     */
    public function status()
    {
        if (request()->isAjax()) {
            $where[] = ['id', '=', input('id')];
            $status = input('status');
            $status += 1;
            try {
                $res = $this->model->where($where)->update(['status' => $status]);
            } catch (\Throwable $th) {
                return $this->error($th->getMessage());
            }
            if ($res) {
                return $this->success();
            } else {
                halt($this->model->getLastSql());
            }
        }

        return $this->error();
    }
}
